Learn to Code HTML & CSS 06
Lesson 6: Working with Typography
Contents
- Adding color to our text to enhance it
- Applying font-based properties, including font-family, font-size, font-style, font-weight, and more
- Applying text-based properties, including text-align, text-decoration, text-indent, text-shadow, and more
- The history behind web-safe fonts and how to embed our own web fonts
How to properly mark up citations and quotations
The field of web typography has grown substantially over time. There are a couple of different reasons for its rise in popularity; one widely acknowledged reason is the development of a system for embedding our own web fonts on a website.
Typeface vs. Font
The terms “typeface” and “font” are often interchanged, causing confusion. Here is a breakdown of exactly what each term means. A typeface is what we see. It is the artistic impression of how text looks, feels, and reads. A font is a file that contains a typeface. Using a font on a computer allows the computer to access the typeface. One way to help clarify the difference between a typeface and a font is to compare them to a song and an MP3.
Adding Color to Text
Typically one of the first decisions we’ll make when building a website is choosing the primary typeface and text color to be used. While there are a number of other properties that can be changed—size, weight, and so on—the typeface and text color generally have the largest impact on the look and legibility of a page.
Changing Font Properties
CSS offers a lot of different properties for editing the look and feel of text on a page. These properties fit into two categories: font-based properties and text-based properties. Most of these properties will be prefaced with either font- or text-.
Font Family
The font-family property is used to declare which font—as well as which fallback or substitute fonts—should be used to display text. The value of the font-family property contains multiple font names, all comma separated.
|
|
Font Size
The font-size property provides the ability to set the size of text using common length values, including pixels, em units, percentages, points, or font-size keywords.
Font Style
To change text to italics, or to prevent text from being italicized, we’ll use the font-style property. The font-style property accepts four keyword values: normal, italic, oblique, and inherit. Of these four, the most commonly used are italic (sets text to italic) and normal (returns text to its normal style).
Font Variant
It doesn’t happen often, but occasionally text will need to be set in small capitals, also known as small caps. For this specific case we’ll use the font-variant property. The font-variant property accepts three values: normal, small-caps, and inherit.
Font Weight
Occasionally, we’ll want to style text as bold or to change the specific weight of a typeface. For these cases we’ll use the font-weight property. The font-weight property accepts either keyword or numeric values.
Keyword values include normal, bold, bolder, lighter, and inherit. Of these keyword values, it is recommended to primarily use normal and bold to change text from normal to bold and vice versa. Rather than using the keyword values bolder or lighter, it’s better to use a numeric value for more specific control.
The numeric values 100, 200, 300, 400, 500, 600, 700, 800, and 900 pertain specifically to typefaces that have multiple weights. The order of these weights starts with the thinnest weight, 100, and scales up to the thickest weight, 900. For reference, the keyword value of normal maps to 400 and the keyword bold maps to 700.
Typeface Weights
Before using a numeric value, we need to check and see whether the typeface we are using comes in the weight we’d like to use. Attempting to use a weight that’s not available for a given typeface will cause those styles to default to the closest value.
Line Height
Line height, the distance between two lines of text (often referred to as leading) is declared using the line-height property. The line-height property accepts all general length values, which we covered in Lesson 3, “Getting to Know CSS.”
The best practice for legibility is to set the line-height to around one and a half times our font-size property value. This could be quickly accomplished by setting the line-height to 150%, or just 1.5. However, if we’re working with a baseline grid, having a little more control over our line-height using pixels may be preferable.
Line height may also be used to vertically center a single line of text within an element. Using the same property value for the line-height and height properties will vertically center the text:
|
|
This technique may be seen with buttons, alert messages, and other single-line text blocks.
Shorthand Font Properties
All of the font-based properties listed earlier may be combined and rolled into one font property and shorthand value. The font property can accept multiple font-based property values. The order of these property values should be as follows, from left to right: font-style, font-variant, font-weight, font-size, line-height, and font-family.
As a shorthand value, these property values are listed from left to right without the use of commas (except for font names, as the font-family property value uses commas). A forward slash, /, separator is needed between the font-size and line-height property values.
When using this shorthand value, every property value is optional except the font-size and font-family property values. That said, we can include only the font-size and font-family property values in the shorthand value if we wish.
|
|
Applying Text Properties
Knowing how to set the family, size, style, variant, weight, and line height of a font is only half the battle. Additionally we can decide how to align, decorate, indent, transform, and space text.
Text Align
Aligning text is an important part of building a rhythm and flow on a page; we do this using the text-align property. The text-align property has five values: left, right, center, justify, and inherit.
Text Decoration
The text-decoration property provides a handful of ways to spruce up text. It accepts the keyword values of none, underline, overline, line-through, and inherit. Use of the text-decoration property varies, but the most popular use is to underline links, which is a default browser style. Multiple text-decoration values may be applied to an element at once by space-separating each keyword within the value.
Text Indent
The text-indent property can be used to indent the first line of text within an element, as is commonly seen in printed publications. All common length values are available for this property, including pixels, points, percentages, and so on. Positive values will indent text inward, while negative values will indent text outward.
Text Shadow
The text-shadow property allows us to add a shadow or multiple shadows to text. The property generally takes four values, all listed one after the other from left to right. The first three values are lengths, and the last value is a color: horizontal offset, vertical offset, blur radius, and shadow color. Using negative length values for the horizontal and vertical offsets allows us to move shadows toward the left and the top.
Multiple text shadows can also be chained together using comma-separated values, adding more than one shadow to the text. Using numerous shadows allows us to place them above and below the text, or in any variation we desire.
Box Shadow
The text-shadow property places a shadow specifically on the text of an element. If we’d like to place a shadow on the element as a whole, we can use the box-shadow property. The box-shadow property works just like the text-shadow property, accepting values for horizontal and vertical offsets, a blur, and a color.
The box-shadow property also accepts an optional fourth length value, before the color value, for the **spread** of a shadow. As a positive length value, the spread will expand the shadow larger than the size of the element it’s applied to, and as a negative length value the spread will shrink the shadow to be smaller than the size of the element it’s applied to.
Lastly, the box-shadow property may include an optional **inset** value at the beginning of the value to place the shadow inside an element as opposed to outside the element.
Text Transform
Similar to the font-variant property, there is the text-transform property. While the font-variant property looks for an alternate variant of a typeface, the text-transform property will change the text inline without the need for an alternate typeface. The text-transform property accepts five values: none, capitalize, uppercase, lowercase, and inherit.
Letter Spacing
Using the letter-spacing property, we can adjust the space (or tracking) between the letters on a page. A positive length value will push letters farther apart from one another, while a negative length value will pull letters closer together. The keyword value none will return the space between letters back to its normal size.
Using a relative length value with the letter-spacing property will help ensure that we maintain the correct spacing between letters as the font-size of the text is changed. It is, however, always a good idea to double-check our work.
Word Spacing
Much like the letter-spacing property, we can also adjust the space between words within an element using the word-spacing property. The word-spacing property accepts the same length values and keywords as the letter-spacing property.
Using Web-Safe Fonts
By default there are a few fonts that are pre-installed on every computer, tablet, smart-phone, or other web-browsing-capable device. Because they’ve been installed on every device, we can use these fonts freely within our websites, knowing that no matter what device is browsing our site, the font will render properly. These fonts have become known as “web-safe fonts.” There are only a handful of them, and the safest of the web-safe fonts are listed here:
- Arial
- Courier New, Courier
- Garamond
- GeorgiaLucida Sans
- Lucida Grande, Lucida
- Palatino Linotype
- Tahoma
- Times New Roman, Times
- Trebuchet
- Verdana
Embedding Web Fonts
We also have the ability to upload fonts to a server and include them on a website via the CSS @font-face at-rule. This capability has done wonders for online typography. Now, more than ever, typography is coming to life online.
Embedding our own web fonts looks a bit like the following CSS. First, we use the @font-face at-rule to identify our font’s name, via the font-family property, as well as the source of our font (the path to the font file containing our chosen font), via the src property. From there we are able to use this font by including its name within any font-family property value.
|
|
Having the ability to embed any typeface on a website does not mean we legally have the authority to do so. Typefaces are works of art, and posting them on our server may allow others to easily steal them. The authority to use a typeface depends on the licensing we’ve been warranted.
Fortunately, the value of using new typefaces online has been recognized, and companies have begun developing ways to license and include new fonts on websites. Some of these companies, like Typekit and Fontdeck, work off a subscription model for licensing fonts, while others, like Google Fonts, license the fonts for free. Before uploading any fonts, let’s make sure we have permission to do so.
Including Citations & Quotes
Writing online sometimes involves citing different sources or quotations. All of the different citation and quotation cases can be covered semantically in HTML using the <cite
>, <q
>, and <blockquote
> elements. Because they are usually distinguished from regular text in appearance, we’ll discuss them here in the typography lesson.
Knowing when to use which element and attribute to properly mark up citations and quotes takes a bit of practice. In general, follow these rules:
<cite
>: Used to reference a creative work, author, or resource<q
>: Used for short, inline quotations<blockquote
>: Used for longer external quotations
Citing a Creative Work
The <cite
> inline element is used in HTML to specifically cite a creative work; the element must include either the title of the work, the author’s name, or a URL reference to the work. By default, content wrapped within the <cite
> element will appear in italics within the browser. For additional reference, it helps to include a hyperlink to the original source of the citation when relevant.
|
|
Dialogue & Prose Quotation
Quite often, dialogue or prose is quoted inline, within other text. For this purpose, the <q
> (or quote) inline element should be applied. The <q
> element semantically indicates quoted dialogue or prose and shouldn’t be used for any other purposes.
By default, the browser will insert the proper quotation marks for us and will even change the quotation marks based on the language identified within the lang global attribute.
|
|
Dialogue & Prose Citation
An optional attribute to include on the <q
> element is the cite attribute. The cite attribute acts as a citation reference to the quote in the form of a URL. This attribute doesn’t alter the appearance of the element; it simply adds value for screen readers and other devices. Because the attribute isn’t viewable within the browser, it’s also helpful to provide a hyperlink to this source next to the actual quotation.
|
|
External Quotation
To quote a large block of text that comes from an external source and spans several lines, we’ll use the <blockquote
> element. The <blockquote
> is a block-level element that may have other block-level elements nested inside it, including headings and paragraphs.
|
|
External Citation
Longer quotes used within the <blockquote
> element will often include a citation. This citation may comprise both the cite attribute and the <cite
> element.
The cite attribute can be included on the <blockquote
> element—in the same way that it was used on the element earlier—to provide a citation reference to the quote in the form of a URL. The
<cite
> element then can fall after the actual quote itself to specify the original source of the quote, if relevant.
|
|
Summary
Learning how to style text is exciting, as our content can begin to convey some emotion. We can also start to play around with the hierarchy of our content, making our website more legible and digestible.
To quickly recap, within this lesson we discussed the following:
- Adding color to our text to enhance it
- Applying font-based properties, including font-family, font-size, font-style, font-weight, and more
- Applying text-based properties, including text-align, text-decoration, text-indent, text-shadow, and more
- The history behind web-safe fonts and how to embed our own web fonts
How to properly mark up citations and quotations